package com.liang.oss.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.liang.oss.entity.Result;
import com.liang.oss.service.FileService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequiredArgsConstructor
public class FileController {

    private final FileService fileService;

    @NacosValue(value = "${storage.type}", autoRefreshed = true)
    private String storageType;

    @RequestMapping("/getBuckets")
    public String getBuckets() {
        return fileService.getAllBucket().get(0);
    }

    @RequestMapping("/testNacos")
    public String testNacos() {
        return storageType;
    }

    /**
     * 上传文件
     */
    @RequestMapping("/upload")
    public Result upload(MultipartFile uploadFile, String bucket, String objectName) throws Exception {
        String url = fileService.uploadFile(uploadFile, bucket, objectName);
        return Result.ok(url);
    }
}
